// import { hasAuthority } from '@/utils/authority-utils';
import { loginIgnore } from '@/router/index';
import { checkAuthorization } from '@/utils/request';
import NProgress from 'nprogress';
import { presOpen } from '@/utils/pres';
// import store from '@/store';
NProgress.configure({ showSpinner: false });

/**
 * 进度条开始
 * @param to
 * @param form
 * @param next
 */
const progressStart = (to, from, next) => {
  // start progress bar
  if (!NProgress.isStarted()) {
    NProgress.start();
  }
  next();
};

/**
 * 登录守卫
 * @param to
 * @param form
 * @param next
 * @param options
 */
const loginGuard = (to, from, next, options) => {
  if (to.path !== '/login' && to.path !== '/') {
    localStorage.setItem('from', to.path);
  }
  const { message } = options;
  if (!loginIgnore.includes(to) && !checkAuthorization()) {
    message.warning('登录已失效，请重新登录');
    setTimeout(() => {
      next({ path: '/login' });
    }, 150);
  } else {
    next();
  }
};

/**
 * 权限守卫
 * @param to
 * @param form
 * @param next
 * @param options
 */
const authorityGuard = (to, from, next, options) => {
  if (to.path !== '/login' && to.path !== '/') {
    localStorage.setItem('from', to.path);
  }

  const { store } = options;
  // const permissions = store.getters['account/permissions'];
  // const roles = store.getters['account/roles'];
  const mrGttter = store.getters['user/menuList'];
  if (!presOpen) {
    next();
  } else {
    if (to.meta?.role === 'allow') {
      next();
    } else if (mrGttter.length === 0) {
      next({ path: '/login' });
    } else if (mrGttter.findIndex(item => item.title === to.meta?.role ?? '') > -1) {
      next();
    } else {
      localStorage.setItem('from', to.path);
      next({ path: '/login' });
    }
  }
};

// if (!hasAuthority(to, permissions, roles)) {
//   message.warning(`对不起，您无权访问页面: ${to.fullPath}，请联系管理员`);
//   next({ path: '/403' });
//   // NProgress.done()
// } else {
//   next();
// }

/**
 * 混合导航模式下一级菜单跳转重定向
 * @param to
 * @param from
 * @param next
 * @param options
 * @returns {*}
 */
const redirectGuard = (to, from, next, options) => {
  const { store } = options;
  const getFirstChild = (routes) => {
    const route = routes[0];
    if (!route.children || route.children.length === 0) {
      return route;
    }
    return getFirstChild(route.children);
  };
  if (store.state.setting.layout === 'mix') {
    const firstMenu = store.getters['setting/firstMenu'];
    if (firstMenu.find(item => item.fullPath === to.fullPath)) {
      store.commit('setting/setActivatedFirst', to.fullPath);
      const subMenu = store.getters['setting/subMenu'];
      if (subMenu.length > 0) {
        const redirect = getFirstChild(subMenu);
        return next({ path: redirect.fullPath });
      }
    }
  }
  next();
};

/**
 * 进度条结束
 * @param to
 * @param form
 * @param options
 */
const progressDone = () => {
  // finish progress bar
  NProgress.done();
};

const rep = (to, from, next, options) => {
  const { store } = options;
  if (to.meta.singlePage) {
    if (to.query?.rp === 'efReportVisible') {
      store.commit('fraMonitoring/setState', { key: 'efReportVisible', payload: true });
    } else if (to.query?.rp === 'detailsVisible') {
      store.commit('faultMonitoring/setState', { key: 'detailsVisible', payload: true });
    } else if (to.query?.rp === 'detailsVisible2') {
      store.commit('faultMonitoring/setState', { key: 'detailsVisible2', payload: true });
    } else if (to.query?.rp === 'faultReportVisible') {
      store.commit('fraMonitoring/setState', { key: 'faultReportVisible', payload: true });
    } else if (to.query?.rp === 'legInfoVisible') {
      store.commit('fraMonitoring/setState', { key: 'legInfoVisible', payload: true });
    } else if (to.query?.rp === 'flightLegDetailsVisible') {
      store.commit('fraMonitoring/setState', { key: 'flightLegDetailsVisible', payload: true });
    } else if (to.query?.rp === 'flightLegHistoryVisible') {
      store.commit('fraMonitoring/setState', { key: 'flightLegHistoryVisible', payload: true });
    } else if (to.query?.rp === 'messageUploadVisible') {
      store.commit('fraMonitoring/setState', { key: 'messageUploadVisible', payload: true });
    } else if (to.query?.rp === 'snapshotVisible') {
      store.commit('fraMonitoring/setState', { key: 'snapshotVisible', payload: true });
    } else if (to.query?.rp === 'eventDetailsVisible') {
      store.commit('fraMonitoring/setState', { key: 'eventDetailsVisible', payload: true });
    } else if (to.query?.rp === 'video') {
      store.commit('setting/setState', { key: 'videoFlag', payload: true });
    }
  }
  next();
};

// // setAcarsTime
// const setAcarsTime = (to, from, next, options) => {
//   const { store } = options;
//   store.dispatch('setting/setAcarsTime');
//   // console.log(store.dispatch('setting/setAcarsTime'))
//   // finish progress bar
//   // if (to.meta.acarsTime) {
//   //   .getters['user/menuList'];
//   // }
//   next();
//   // const mrGttter =
// };

export default {
  beforeEach: [progressStart, loginGuard, authorityGuard, redirectGuard, rep],
  afterEach: [progressDone]
};
